<?php

/**
 * Object/Day.php
 */

namespace RHX\Model\Object;

use RHX\Model\Object\PlanningSemaine;
use Zend\Db\Adapter\Adapter;
use RHX\Helper\RHDateTime;
use RHX\Model\Object\XRules;

class Day {
    /**
     *
     * @var int
     */
    public $matricule;

    /**
     *
     * @var string
     */
    public $name;

    /**
     *
     * @var string
     */
    public $date;

    /**
     *
     * @var int
     */
    public $code;

    /**
     *
     * @var RHDateTime
     */
    public $entree;

    /**
     *
     * @var RHDateTime
     */
    public $sortie;

    /**
     *
     * @var int
     */
    public $pause;

    /**
     *
     * @var int
     */
    public $repas;

    /**
     *
     * @var RHDateTime
     */
    public $refEntree;

    /**
     *
     * @var RHDateTime
     */
    public $refSortie;
    // Base de données
    /**
     *
     * @var Adapter
     */
    protected $adapter;

    /**
     *
     * @var \RHX\Model\Table\CodeActiviteTable
     */
    protected $tableCode;

    public function __construct(Adapter $adapter, PlanningSemaine $planning, $name) {
	$this->adapter	 = $adapter;
	$dayCode	 = $name . '_code';
	$dayEntree	 = $name . '_entree';
	$daySortie	 = $name . '_sortie';
	$dayPause	 = $name . '_pause';
	$dayRepas	 = $name . '_repas';
	$dayRefEntree	 = $name . '_entree_ref';
	$dayRefSortie	 = $name . '_sortie_ref';
	$this->name	 = $name;
	$this->date	 = $planning->$name;
	$this->code	 = (int) $planning->$dayCode;
	$this->entree	 = new RHDateTime();
	$this->entree
		->setTime($planning->$dayEntree);
	$this->sortie	 = new RHDateTime();
	$this->sortie
		->setTime($planning->$daySortie);
	$this->pause	 = (int) $planning->$dayPause;
	$this->repas	 = (int) $planning->$dayRepas;
	$this->refEntree = new RHDateTime();
	$this->refEntree
		->setTime($planning->$dayRefEntree);
	$this->refSortie = new RHDateTime();
	$this->refSortie
		->setTime($planning->$dayRefSortie);
	$this->matricule = (int) $planning->matricule;
	// Object de la base de données
	$this->tableCode = new \RHX\Model\Table\CodeActiviteTable($this->adapter);
    }

    /**
     * Récupération du code
     *
     * @return CodeActivite|null
     */
    public function getCode() {
	try {
	    $code = $this->tableCode
		    ->get($this->code);
	    return $code;
	}
	catch (\Exception $ex) {
	    return null;
	}
    }

    /**
     * Couleur des champs
     * 
     * @return array
     */
    public function getColor() {
	$color = array(
	    'code'	 => '#FFFFFF',
	    'entree' => '#FFFFFF',
	    'sortie' => '#FFFFFF',
	);
	if ($this->code == XRules::ProblemeRetardDepartAnticipe) {
	    //
	}
	else {
	    $code = $this->getCode();
	    if (!is_null($code)) {
		$color['code']	 = $color['entree'] = $color['sortie'] = $code->couleur;
		$prda		 = $this->whichIsLate();
		if ($prda['retard']) {
		    $retard		 = $this->tableCode
			    ->get(XRules::Retard);
		    $color['entree'] = $retard->couleur;
		}
		if ($prda['anticipe']) {
		    $anticipe		 = $this->tableCode
			    ->get(XRules::DepartAnticipe);
		    $color['anticipe']	 = $anticipe->couleur;
		}
	    }
	}

	return $color;
    }

    /**
     * Détermine lequel est en PRDA
     *
     * @return array
     */
    public function whichIsLate() {
	$result = array(
	    'retard'	 => false,
	    'anticipe'	 => false,
	);
	if ($this->entree->isGT($this->refEntree)) {
	    $result['retard'] = true;
	}
	if ($this->sortie->isLT($this->refSortie)) {
	    $result['anticipe'] = true;
	}

	return $result;
    }

    /**
     * Représentation de la pause
     *
     * @return string
     */
    public function showPause() {
	return ($this->pause == 1) ? 'X' : '';
    }

    /**
     * Représentation du repas$
     *
     * @return string
     */
    public function showRepas() {
	return ($this->repas == 1) ? '1' : '';
    }

    /**
     * Heure d'entrée
     *
     * @return string
     */
    public function showEntree() {
	return $this->entree
			->hours();
    }

    /**
     * Heure de sortie
     * 
     * @return string
     */
    public function showSortie() {
	return $this->sortie
			->hours();
    }

}

?>
